from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from meiduo_admin.serializers.images import ImageSerializer, SKUSerializer
from meiduo_admin.utils import PageNum
from goods.models import SKUImage, SKU


class ImageView(ModelViewSet):
    """
        图片表的增删改查
    """
    # 指定序列化器
    serializer_class = ImageSerializer
    # 指定查询集
    queryset = SKUImage.objects.all()
    # 指定分页器
    pagination_class = PageNum
    # 指定权限
    permission_classes = [IsAdminUser]


    # 在保存图片之前先获取sku数据
    # @action(methods=['get'],detail=False)
    def simple(self, request):
        # 1、获取sku数据
        skus = SKU.objects.all()
        # 2、序列化返回sku数据
        ser = SKUSerializer(skus, many=True)

        return Response(ser.data)